var ArachneSlideShow = {
	getAnchorId: function(anchor) {
		var id = anchor.href.substr(anchor.href.indexOf("#") + 1);
		return id;
	}
	,addStyles:function(styles) {
		var head = document.getElementsByTagName('head')[0],
		    style = document.createElement('style'),
		    rules = document.createTextNode(styles);
		
		style.type = 'text/css';
		head.appendChild(style);
		if(style.styleSheet)
		    style.styleSheet.cssText = rules.nodeValue;
		else style.appendChild(rules);
	}
	, scrubShow: function(show) {
		show.className+=" init";
		for (var i = 0; i < show.childNodes.length; i++) {
			var node = show.childNodes[i];
			if (!node.childNodes || node.childNodes.length <= 0) {
				show.removeChild(node);
				i--;
			}
		}
		window.setTimeout(function(){show.className+=" ready";},0);
		show.firstChild.className += " active";
	}
	, setArticleClass: function(show, n, className) {
		if (n < 0) {
			ArachneSlideShow.setArticleClass(show, show.childNodes.length + n, className);
		} else if (n >= show.childNodes.length) {
			ArachneSlideShow.setArticleClass(show, n - show.childNodes.length, className);
		} else {
			show.childNodes[n].className += " " + className;
		}
	}
	, incSlide: function(id, inc) {
		var show = document.getElementById(id);
		if (show) {
			for (var i = 0, n = 0; i < show.childNodes.length; i++) {
				var node = show.childNodes[i];
				if (node.className.indexOf("active") > 0) {
					node.className = node.className.replace("active", "");
					n = i;
				}
				if (node.className.indexOf("prevArticle") > 0) {
					node.className = node.className.replace("prevArticle", "");
				}
			}
			var nn = n + inc;
			ArachneSlideShow.setArticleClass(show, nn - 1, "prevArticle");
			ArachneSlideShow.setArticleClass(show, nn, "active");
		}
	}
	, initLightBoxes: function() {
		var boxes = document.getElementsByTagName("div");
		for (var i = 0; i < boxes.length; i++) {
			var box = boxes[i];
			if (box.className == "lbcss3") {
				ArachneSlideShow.scrubShow(box.getElementsByTagName("ul")[0]);
			}
		}
	}
	, startAutorun: function(id, duration) {
		var show = $(id);
		if (show) {
			show.timer = window.setInterval(function(id) {
				ArachneSlideShow.incSlide(id,1);
			}, duration, id);
		}
	}
	, stopAutorun: function(id) {
		var show = $(id);
		if (show && show.timer) {
			window.clearInterval(show.timer);
			show.timer = false;
		}
	}
}

LinkListener.addHandler("lbctrl_prev",function(link) {
	var id = ArachneSlideShow.getAnchorId(link);
	ArachneSlideShow.incSlide(id,-1);
	ArachneSlideShow.stopAutorun(id)
	return true;
});

LinkListener.addHandler("lbctrl_next",function(link) {
	var id = ArachneSlideShow.getAnchorId(link);
	ArachneSlideShow.incSlide(id,1);
	ArachneSlideShow.stopAutorun(id)
	return true;
});

if (Tapestry.Initializer) {
	Tapestry.Initializer.ArachneSlideShow = function(p) {
		//Some styling is dynamic, based on the dimensions of the slide-show...
		var aw = (parseFloat(p.width) - parseFloat(p.padding) * 2);
		var styles = 
	          "#" + p.id + ".init .slide { margin-right: -" + p.width + p.unit + "; width: " + aw + p.unit + "; }" + "\n"
	        + "#" + p.id + ".init .slide * { max-width: " + aw + p.unit + "; }" + "\n"
	        + "#" + p.id + "_parent { width: " + (parseFloat(p.width) + parseFloat(p.offset) * 2) + p.unit + "; }" + "\n"
	        + "#" + p.id + " { width: " + p.width + p.unit + "; }"
	        + "#" + p.id + "_parent .ready * { -webkit-transition-duration: " + p.duration + "; -moz-transition-duration: " + p.duration + "; -o-transition-duration: " + p.duration + "; transition-duration: " + p.duration + "; " + "\n"
    		+ "                                -webkit-transition-property: " + p.transition + "; -moz-transition-property: " + p.transition + "; -o-transition-property: " + p.transition + "; transition-property: " + p.transition + "; " + "\n"
    		+ "                                -webkit-transition-timing-function: " + p.timing + "; -moz-transition-timing-function: " + p.timing + "; -o-transition-timing-function: " + p.timing + "; transition-timing-function: " + p.timing + "; }" + "\n";
    	if (p.transition.indexOf("margin") >= 0) {
    		styles +=
					  "#" + p.id + ".init li.prevArticle { margin-left: -" + p.width + p.unit + "; }" + "\n"
					+ "#" + p.id + ".init li.active ~ li.prevArticle { margin-left: -" + parseFloat(p.width) * 2 + p.unit + "; }" + "\n"
    				+ "#" + p.id + ".init li.active { margin-right: 0; }" + "\n";
		}

        ArachneSlideShow.addStyles(styles);
        
        //Now for cleaning up the dom and activating the styles...
		ArachneSlideShow.scrubShow($(p.id));
		
		//In case autorun is defined, start the show!
		if (p.autorun && p.autorun > 0) {
			ArachneSlideShow.startAutorun(p.id, p.autorun);
		}
	}
} else {
	alert("Tapestry library not available!");
}